import { wxRequest } from "../../utils/httpRequest"
import { sortBy, getDistance } from "../../utils/util"
const app = getApp()

Page({
    data: {
        inputSearchValue: "",
        shopList: []
    },
    searchShopByKeywords(searchKeywords) {
        let url = "searchShopByKeywords"
        let params = {
            keywords: searchKeywords
        }
        wxRequest(url, params).then((res) => {
            let list = res.data;
            wx.getLocation({
                type: 'wgs84',
                success: (res)=> {
                    list.map((item) => {
                        item.distance = getDistance(item.latitude, item.longitude, res.latitude, res.longitude);
                        return item;
                    });
                    this.setData({ shopList: list.sort(sortBy("distance")).reverse()});
                }
            })
        });
    },
    fieldValueChange(event) {
        this.setData({ inputSearchValue: event.detail });
    },
    goShopMeals(event) {
        wx.navigateTo({
            url: `/home_package/shopMeals/shopMeals?shopId=${event.currentTarget.dataset.shopid}`,
        });
    },
    goSearch() {
        this.searchShopByKeywords(this.data.inputSearchValue);
    },
    onLoad(options) {
        this.setData({ inputSearchValue: options.searchKeywords });
        this.searchShopByKeywords(options.searchKeywords);
    }
})